﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ReconUI
{
    public partial class CustomerMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((UserVO)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //`is_fileencryptenabled` bit(1) default NULL,  //`is_pgpenabled` bit(1) default NULL,
            //`is_remitter_availble` bit(1) default NULL,  //`bank_id` varchar(10) default NULL,  
            //`is_remittercode_avaiable` bit(1) default NULL,  //`is_autoreconciliation` bit(1) default NULL,

            string resp = string.Empty;
            DBFactory db = new DBFactory();
            DataTable dtCustomerDetails = new DataTable();
            int is_Enabled = 0;
           
            if (chkIsenabled.Checked)
                is_Enabled = 1;
            
            if (rbOption.SelectedValue == "1")
            {

                 dtCustomerDetails = db.GetCustomerDetailsByCustomerId(Convert.ToString(txtCustomerId.Text.Trim()));

                 if (dtCustomerDetails.Rows.Count == 1)
                 {
                     //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer already exist!')</script>");
                     lblMessage.Text = "Customer already exist!";
                     btnClickPopup.Show();
                 }
                 else
                 {
                     //string customerID = System.DateTime.Now.ToString("MMddHHmmss");
                     int retval = db.AddCustomer(Convert.ToString(txtCustomerId.Text.Trim()), Convert.ToString(txtName.Text.Trim()), Convert.ToString(txtAddress1.Text.Trim()), Convert.ToString(txtAddress2.Text.Trim()), Convert.ToString(txtCity.Text.Trim()), Convert.ToString(txtState.Text.Trim()), Convert.ToString(txtCountry.Text.Trim()), Convert.ToString(txtZipCode.Text.Trim()), Convert.ToString(txtPhone.Text.Trim()), Convert.ToString(txtEmail.Text.Trim()), is_Enabled, ((UserVO)Session["CurrentUserVO"]).userId);

                     if (retval != 0)
                     {
                         //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer added successfully!')</script>");
                         lblMessage.Text = "Customer added successfully!";
                         btnClickPopup.Show();
                         ResetFields();
                     }
                     else
                     {
                         //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error adding Customer!')</script>");
                         lblMessage.Text = "Error adding Customer!";
                         btnClickPopup.Show();
                     }
                 }
            }
            else
            {
                dtCustomerDetails = db.GetCustomerDetailsByCustomerId(Convert.ToString(txtCustomerId.Text.Trim()));

                if (dtCustomerDetails.Rows.Count <= 0)
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer does not exist!')</script>");
                    lblMessage.Text = "Customer does not exist!";
                    btnClickPopup.Show();
                }
                else if (dtCustomerDetails.Rows.Count == 1)
                {
                    int retval = db.ModifyCustomer(Convert.ToString(txtCustomerId.Text.Trim()), Convert.ToString(txtName.Text.Trim()), Convert.ToString(txtAddress1.Text.Trim()), Convert.ToString(txtAddress2.Text.Trim()), Convert.ToString(txtCity.Text.Trim()), Convert.ToString(txtState.Text.Trim()), Convert.ToString(txtCountry.Text.Trim()), Convert.ToString(txtZipCode.Text.Trim()), Convert.ToString(txtPhone.Text.Trim()), Convert.ToString(txtEmail.Text.Trim()), is_Enabled, ((UserVO)Session["CurrentUserVO"]).userId);

                    if (retval != 0)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Customer modified sucessfully!')</script>");
                        lblMessage.Text = "Customer modified sucessfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error Modifying Customer!')</script>");
                        lblMessage.Text = "Error Modifying Customer!";
                        btnClickPopup.Show();
                    }
                
                }

            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        private void ResetFields()
        {
            rbOption.SelectedValue = "1";
            txtCustomerId.Text = "";
            txtName.Text = "";
            txtAddress1.Text = "";
            txtAddress2.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            txtCountry.Text = "";
            txtZipCode.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
            chkIsenabled.Checked = true;
        }

        protected void bntbrowse_Click(object sender, EventArgs e)
        {
            string path;
            path = "CustomerMaster.aspx";
            // txtUserId.Text - Get the value ctl00$BodyHolder$txtUserId from Unique Id of the control txtUserId
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchCustomer.aspx?textbox=" + HttpUtility.UrlEncode(txtCustomerId.Text) + "&PageRequest= " + path + "','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        }

        protected void bntgetdetails_Click(object sender, EventArgs e)
        {
            DataTable dtCustomerDetails = new DataTable();
            DBFactory objDBfactory = new DBFactory();

            try
            {

                //select  customer_id,customer_name, customer_address1, customer_address2,customer_city,customer_state, customer_country,
                //customer_zipcode,customer_telno,customer_emailid,is_enabled 
                if (txtCustomerId.Text.Trim() != "")
                {
                    dtCustomerDetails = objDBfactory.GetCustomerDetailsByCustomerId(Convert.ToString(txtCustomerId.Text.Trim()));

                    if ((dtCustomerDetails) == null)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('No record found for this customer!')</script>");
                        lblMessage.Text = "No record found for this customer!";
                        btnClickPopup.Show();
                    }
                    else if (dtCustomerDetails.Rows.Count == 1)
                    {
                        ResetFields();

                        txtCustomerId.Text = dtCustomerDetails.Rows[0].ItemArray[0].ToString();
                        txtName.Text = dtCustomerDetails.Rows[0].ItemArray[1].ToString();
                        txtAddress1.Text = dtCustomerDetails.Rows[0].ItemArray[2].ToString();
                        txtAddress2.Text = dtCustomerDetails.Rows[0].ItemArray[3].ToString();
                        txtCity.Text = dtCustomerDetails.Rows[0].ItemArray[4].ToString();
                        txtState.Text = dtCustomerDetails.Rows[0].ItemArray[5].ToString();
                        txtCountry.Text = dtCustomerDetails.Rows[0].ItemArray[6].ToString();
                        txtZipCode.Text = dtCustomerDetails.Rows[0].ItemArray[7].ToString();
                        txtPhone.Text = dtCustomerDetails.Rows[0].ItemArray[8].ToString();
                        txtEmail.Text = dtCustomerDetails.Rows[0].ItemArray[9].ToString();
                        
                        if (dtCustomerDetails.Rows[0].ItemArray[10].ToString() == "True")
                            chkIsenabled.Checked = true;
                        else
                            chkIsenabled.Checked = false;
                        
                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('No record found for this customer!')</script>");
                        lblMessage.Text = "No record found for this customer!";
                        btnClickPopup.Show();
                    }
                }
                else
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Enter Customer ID!')</script>");
                    lblMessage.Text = "Enter Customer ID!";
                    btnClickPopup.Show();

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if ((dtCustomerDetails) != null)
                {
                    dtCustomerDetails.Dispose();
                }
                dtCustomerDetails = null;

            }
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }
    }
}